//
//  DKHomeTableViewCell.swift
//  Sina
//
//  Created by 赵柏清 on 16/8/4.
//  Copyright © 2016年 DarkZhao. All rights reserved.
//

import UIKit
import SnapKit

class DKHomeTableViewCell: UITableViewCell {
    /// 这是一个全局的约束
    var constraint : Constraint?
    /// 给cell的模型数据
    var cellModel : DKHomeCellViewModel? {
        // 当cell有值时,我们再给原创微博赋值
        didSet {
            let number = ((cellModel!).statusModel!).comments_count
            if number != 0 {footerView.turnCount = "\(number)"}
            
            orignalView.orignalCellModel = cellModel
            retWeetView.retModel = cellModel
            
            // 赋值的时候我们将之前的约束去掉(卸载)
            constraint?.uninstall()
            
            if cellModel?.statusModel?.retweeted_status == nil {
                // 如果没有转发微博,则隐藏此view
                retWeetView.hidden = true
                
                footerView.snp_updateConstraints(closure: { (make) -> Void in
                    self.constraint = make.top.equalTo(orignalView.snp_bottom).constraint
                })
                
            } else {
                
                retWeetView.hidden = false
                
                footerView.snp_updateConstraints(closure: { (make) -> Void in
                    self.constraint = make.top.equalTo(retWeetView.snp_bottom).constraint
                })
            }
            
            
        }
    }
    
    
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("没有实现此方法")
    }
    
    private func setupUI() {
        
        contentView.addSubview(orignalView)
        contentView.addSubview(retWeetView)
        contentView.addSubview(footerView)
        
        orignalView.snp_makeConstraints { (make) -> Void in
            make.left.top.right.equalTo(contentView)
        }
        
        retWeetView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(orignalView.snp_bottom)
            make.left.right.equalTo(contentView)
        }
        
        footerView.snp_makeConstraints { (make) -> Void in
            self.constraint = make.top.equalTo(retWeetView.snp_bottom).constraint
            make.left.right.equalTo(self)
            make.height.equalTo(44)
        }
        
        
        contentView.snp_makeConstraints { (make) -> Void in
            make.left.right.top.equalTo(self)
            // 自动计算行高是的时候,要确定底部约束(以哪个控件作为底部结束)
            make.bottom.equalTo(footerView.snp_bottom)
        }
        
    }
    
    // MARK: 懒加载
    lazy var orignalView : DKOrignalView = DKOrignalView()
    lazy var retWeetView : DKRetWeetView = DKRetWeetView()
    lazy var footerView : DKFooterView = DKFooterView()

}
